package com.xindz.hgq.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.kingja.loadsir.callback.Callback;
import com.xindz.hgq.R;
import com.xindz.hgq.adapter.SimpleAdapter;
import com.xindz.hgq.base.BasePresenter;
import com.xindz.hgq.base.BaseTitleActivity;
import com.xindz.hgq.base.BaseTitleToolBarActivity;
import com.xindz.hgq.base.BaseView;
import com.xindz.hgq.base.MvpInterface;
import com.xindz.hgq.base.options.BaseActivityOptions;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

/**
 * @name HGQBase
 * @class name：com.xindz.hgq.activity
 * @describe describe
 * @anthor XINDZ
 * @time 2018/1/16 10:55
 * @change
 * @chang time
 * @class describe
 */
public class ShowAllActivity extends BaseTitleToolBarActivity<MvpView, MvpPresenter> implements MvpView, MvpInterface, Callback.OnReloadListener {
    @BindView(R.id.rcv_titleone)
    RecyclerView rcvTitleone;
    private Unbinder bind;

    private ArrayList<String> dataList = new ArrayList<>();
    private SimpleAdapter simpleAdapter;

    @Override
    public BaseActivityOptions setOption() {
        return new BaseActivityOptions().openMVP(this).openLoadSir(this).openTitle();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_title_one);
        bind = ButterKnife.bind(this);

        initView();

        initData();
        Observable.timer(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        showLoadSirEmpty();
                    }
                });

    }

    private void getData() {
        Observable.timer(2, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        for (int i = 0; i < 15; i++) {
                            int a = i + 1;
                            dataList.add("第 " + a + "    条");
                        }
                        simpleAdapter.notifyDataSetChanged();
                        showLoadSirSuccess();
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_one, menu);
        return true;
    }

    private void initView() {
        /**
         * 右侧Menu 点击事件
         */
        setMenuOnClickListerner(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                getData();
                return true;
            }
        });
        /**
         * 展示左侧返回键 并且点击finish界面
         */
        showLeftBack();
        /**
         * 设置Title可以滚出界面
         */
        setToolBarScroll();
    }

    private void initData() {
        rcvTitleone.setLayoutManager(new LinearLayoutManager(this));
        dataList.clear();
        dataList.add("Title模式使用方法见注释");
        simpleAdapter = new SimpleAdapter(R.layout.item_simple, dataList);
        rcvTitleone.setAdapter(simpleAdapter);
    }

    @Override
    public void onReload(View v) {
        getPresneter().showToast("点击ReLoad");
        showLoadSirLoading();

    }

    @Override
    public BasePresenter createPresneter() {
        return new MvpPresenter();
    }

    @Override
    public BaseView createView() {
        return this;
    }

    @Override
    public void showToase(String msg) {
        ToastUtils.showShort(msg);
        getData();
    }
}
